package com.example.pizzayolo;

import java.util.ArrayList;

import android.util.Log;


public final class CommandeClient {

	String contenu = "";
	private static volatile CommandeClient instance = null;
	public ListPizza pizzaCommande = new ListPizza();
	ArrayList<Pizza> majPizza = new ArrayList<Pizza>();
	static ArrayList<PizzaDetail> persoPizza = new ArrayList<PizzaDetail>();
	double prixTotal = 0;

	public double getPrixTotal() {
		return prixTotal;
	}

	public ArrayList<PizzaDetail> getPersoPizza() {
		return persoPizza;
	}

	public ArrayList<Pizza> getMajPizza() {
		return majPizza;
	}

	public void setMajPizza(ArrayList<Pizza> majPizza) {
		this.majPizza = majPizza;
	}

	public CommandeClient() {
		super();
	}

	public final static CommandeClient getInstance() {
		if (CommandeClient.instance == null) {
			CommandeClient.instance = new CommandeClient();
		}
		return CommandeClient.instance;
	}
	
	public void loadListPizzas()
	{
		pizzaCommande.LoadList();
		majPizza = pizzaCommande.getPizzas();
	}
	
	 public Pizza GetbyId(int Id){

	        for(Pizza pizza : majPizza) {
	            if (pizza.id == Id) {
	                return pizza;
	            }
	        }
	        return null;
	    }
	 
	/*public void majCommande(int id, int quantite) {
		majPizza.add(new Pizza(id,pizzaCommande.get(id).nom, pizzaCommande.get(id).ingredients,pizzaCommande.get(id).prix,quantite, pizzaCommande.get(id).image));
		Pizza maj = new Pizza(id,pizzaCommande.get(id).nom, pizzaCommande.get(id).ingredients,pizzaCommande.get(id).prix,quantite, pizzaCommande.get(id).image);
		pizzaCommande = pizzaCommande.set(id, maj);		
	}*/

	public void majListCommand(int position, int which) {
		// TODO Auto-generated method stub
		majPizza.set(position, new Pizza(majPizza.get(position).id,majPizza.get(position).nom, majPizza.get(position).ingredients,majPizza.get(position).prix,which, majPizza.get(position).image));
	}
	
	public void personnalisationPizza()
	{

		persoPizza = new ArrayList<PizzaDetail>();
		int idList = 1;
		for(int i = 0; i < majPizza.size(); i++)
		{
			if (majPizza.get(i).quantite > 0)
			{
				for(int quantite = 0; quantite < majPizza.get(i).quantite; quantite++)
				{
					
					Log.e("ADD PIZZA","OK i = " + i + " quantite " + quantite +" nom " + majPizza.get(i).nom);
					persoPizza.add(new PizzaDetail(idList,majPizza.get(i).nom, "Normale","Normale",majPizza.get(i).prix, majPizza.get(i).image));
					idList++;
				}
			}
		}
	}
	
	public void personnalisationPatePizza(int choix, int which)
	{
		String typePate = null;
		switch (which)
		{
		case 0 : typePate = "Fine"; break;
		case 1 : typePate = "Normale"; break;
		case 2 : typePate = "Epaisse"; break;
		default : break;
		}
		if(persoPizza.get(choix).pate != "Epaisse" && typePate == "Epaisse" )
		{
			persoPizza.get(choix).prix = persoPizza.get(choix).prix + 1;
		}
		if(persoPizza.get(choix).pate == "Epaisse" && typePate != "Epaisse" )
		{
			persoPizza.get(choix).prix = persoPizza.get(choix).prix - 1;
		}
		persoPizza.get(choix).pate = typePate;
		
	}
	
	public void personnalisationTaillePizza(int choix, int which)
	{
		String typeTaille = null;
		switch (which)
		{
		case 0 : typeTaille = "Normale"; break;
		case 1 : typeTaille = "Grande"; break;
		default : break;
		}
		
		if(persoPizza.get(choix).taille == "Normale" && typeTaille == "Grande" )
		{
			persoPizza.get(choix).prix = persoPizza.get(choix).prix + 2;
		}
		if(persoPizza.get(choix).taille == "Grande" && typeTaille == "Normale" )
		{
			persoPizza.get(choix).prix = persoPizza.get(choix).prix - 2;
		}
		persoPizza.get(choix).taille = typeTaille;
		
	}
	
	public PizzaDetail GetbyIdPerso(int Id){

        for(PizzaDetail pizza : persoPizza) {
            if (pizza.id == Id) {
                return pizza;
            }
        }
        return null;
    }
	
	public void calculPrixTotal()
	{
		prixTotal = 0;
		for(int i = 0; i < persoPizza.size(); i++)
		{
			prixTotal = prixTotal + persoPizza.get(i).prix;
		}
	}
	
	/*public void validerCommande(int id, int quantite) {
	}*/
	
}
